package jmini.examples.orm;

import java.io.IOException;
import java.util.List;

import jmini.dto.Pagination;
import jmini.examples.modules.People;
import jmini.examples.modules.Pet;
import jmini.examples.spring.SpringEnv;
import jmini.fw.MiniHelper;
import jmini.iservice.IMiniService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleExample {
	
	public static Logger logger = LoggerFactory.getLogger(SimpleExample.class);
	
	public static void main(String[] args) throws IOException {
		SpringEnv.init();
		MiniHelper.setOperatorId("1");//默认的操作员ID，实际应用中可以通过拦截器从session中注入用户id
		//设置测试数据
		initData();
		//创建数据
		createExample();
		//更新数据
		updateExample();
		//查询数据
		queryExample();
		//删除数据
		deleteExample();
		SpringEnv.destroy();
	}
	
	public static void initData(){
		IMiniService service = SpringEnv.getMiniService();
		service.updateBySQL("TRUNCATE TABLE PEOPLE");
		service.updateBySQL("TRUNCATE TABLE PET");
		
		People czy = new People();
		czy.setId("1");
		czy.setName("操作员");
		czy.setGender("男");
		service.save(czy);
		
		People zhangsan = new People();
		zhangsan.setId("2");
		zhangsan.setName("张三");
		zhangsan.setGender("男");
		service.save(zhangsan);
		
		People lisi = new People();
		lisi.setId("3");
		lisi.setName("李四");
		lisi.setGender("男");
		service.save(lisi);
		
	}
	
	public static void createExample(){
		IMiniService service = SpringEnv.getMiniService();
		//清除历史数据
		service.updateBySQL("DELETE FROM PEOPLE WHERE NAME=?", "李明");
		service.updateBySQL("DELETE FROM PET WHERE NAME=?", "foo");
		service.updateBySQL("DELETE FROM PET WHERE NAME=?", "bar");
		
		People liming = new People();
		liming.setName("李明");
		liming.setGender("男");
		service.save(liming);
		logger.info("创建用户 {} 成功 id={}", liming.getName(), liming.getId());
		
		Pet foo = new Pet();
		foo.setName("foo");
		foo.setGender("雄");
		foo.setOwnerId(liming.getId());
		service.save(foo);
		logger.info("创建宠物 {} 成功 id={}", foo.getName(), foo.getId());
		
		Pet bar = new Pet();
		bar.setName("bar");
		bar.setGender("雌");
		bar.setOwnerId(liming.getId());
		service.save(bar);
		logger.info("创建宠物 {} 成功 id={}", bar.getName(), bar.getId());
	}
	
	public static void updateExample(){
		IMiniService service = SpringEnv.getMiniService();
		//通过查询更新
		People zhangsan = service.query("2", People.class);
		zhangsan.setGender("女");
		service.update(zhangsan);
		zhangsan = service.query("2", People.class);
		logger.info("通过查询更新 成功  名字={},性别={},id={}", zhangsan.getName(), zhangsan.getGender(), zhangsan.getId());
		
		//指定字段更新
		zhangsan.setName("李四");
		service.update(zhangsan, "NAME");
		zhangsan = service.query("2", People.class);
		logger.info("指定字段更新 成功  名字={},性别={},id={}", zhangsan.getName(), zhangsan.getGender(), zhangsan.getId());
		
		//通过SQL更新
		service.updateBySQL("UPDATE PEOPLE SET NAME=? WHERE ID=?", "张三2", zhangsan.getId());
		zhangsan = service.query("2", People.class);
		logger.info("通过SQL更新 成功  名字={},性别={},id={}", zhangsan.getName(), zhangsan.getGender(), zhangsan.getId());
	}
	
	public static void queryExample(){
		IMiniService service = SpringEnv.getMiniService();
		//通过ID查询 操作员
		People czy = service.query("1", People.class);
		logger.info("查询操作员完成  名字={},性别={},id={}", czy.getName(), czy.getGender(), czy.getId());
		
		/* 通过Module配置
		 *  People.class
		 *  @AP_ONE2MANY(modelType=Pet.class, fieldName="OWNER_ID")
		 *  private List<Pet> pets = null;
		 *  测试此方法前请先使用createExample()方法创建测试数据
		 *  queryOne 参数说明
		 *  whereStr sql语句的where条件
		 *  orderStr sql语句的order条件
		 *  requiredType 查询的模型类
		 *  args sql的参数
		*/
		People liming = service.queryOne("NAME=? ", null, People.class, "李明");
		logger.info("查询  李明 成功 有宠物 {} {}", liming.getPets().get(0).getName(), liming.getPets().get(1).getName());
		
		List<People> peoples = service.queryList("GENDER=? ", null, People.class, "男");
		logger.info("查询  成功 有{}个男性用户.", peoples.size());
		
		//分页查询
		//默认Pagination.size为10，可以自行设置
		Pagination page = new Pagination();
		service.queryByPage(page, "GENDER=? ", "CREATE_DATE DESC", People.class, "男");
		peoples = (List<People>) page.getDatas();
		logger.info("分页查询  成功 有{}个男性用户.", peoples.size());
		
		//sql分页查询
		//这里可以实现多表，自定义Module数据查询
		service.queryBySQLPage(page, "SELECT COUNT(ID) FROM PEOPLE",
				"SELECT * FROM PEOPLE", "GENDER=? ", "CREATE_DATE DESC", 
				People.class,
				"男");
		logger.info("sql分页查询  成功 有{}个男性用户.", peoples.size());
	}
	
	public static void deleteExample(){
		IMiniService service = SpringEnv.getMiniService();
		People lisi = service.query("3", People.class);
		service.delete(lisi);
		/*也可以直接用id删除
		 * People lisi = new People();
		 * lisi.setId("3");
		 * service.delete(lisi);
		 */
	}

}
